package ru.jexp.rcp.supply.beans.account;

import java.util.Date;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import ru.jexp.kontragent.Kontragent;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.swtext.JExpChangeElementEvent;
import ru.jexp.rcp.supply.swtext.JExpChangeElementListener;
import ru.jexp.rcp.supply.swtext.JExpControlLookup;
import ru.jexp.rcp.supply.swtext.JExpDate;
import ru.jexp.rcp.supply.swtext.JExpText;
import ru.jexp.rcp.supply.swtext.factory.JExpControlFactory;
import ru.jexp.rcp.supply.swtext.factory.JExpDecorationFactory;
import ru.jexp.rcp.supply.swtext.factory.JExpLookupFactory;
import ru.jexp.rcp.supply.wizard.JExpWizard;
import ru.jexp.utilities.StringUtil;


public final class WizardPageDocParams extends WizardPage {

	public static final int CHANGE_PARAMS = -25;
	
	private JExpDate dateReg;
	private JExpText number;
	private JExpControlLookup supplierLookup;

	public WizardPageDocParams(String pageName) {
		super(pageName);
		setTitle(pageName);
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		InnerChangeListener listener = new InnerChangeListener();
		JExpControlFactory.createDecorationLabel(composite,
				JExpSupplyMessages.Global_field_number);
		number = JExpControlFactory.createText(composite);
		number.addModifyListener(listener);
		JExpControlFactory.createDecorationLabel(composite,
				JExpSupplyMessages.Global_field_date);
		dateReg = JExpControlFactory.createDate(composite);
		dateReg.addSelectionListener(listener);
		supplierLookup = JExpLookupFactory.createSupplierLookup(composite, 200);
		supplierLookup.addChangeElementListener(listener);
		JExpDecorationFactory.labelDecoration(supplierLookup.getLabel());
		setControl(composite);
	}

	public Date getDate() {
		return dateReg.getValue();
	}

	public String getNumber() {
		return number.getValue();
	}

	public Kontragent getSuppler() {
		return (Kontragent) supplierLookup.getSelectedElement();
	}

	public boolean canFinish() {
		return !StringUtil.isEmptyWithoutSpace(number.getValue())
				&& dateReg.getValue() != null
				&& supplierLookup.getSelectedElement() != null;
	}
	
	private void fireWizardListeners(int typeEvent)
	{
		getJExpWizard().fireWizardListeners(typeEvent);	
	}
	
	private JExpWizard getJExpWizard() {
		return (JExpWizard) getWizard();
	}

	private class InnerChangeListener extends SelectionAdapter implements
			ModifyListener, JExpChangeElementListener {

		@Override
		public void widgetSelected(SelectionEvent e) {
			WizardPageDocParams.this
					.fireWizardListeners(WizardPageDocParams.CHANGE_PARAMS);
		}

		@Override
		public void modifyText(ModifyEvent e) {
			WizardPageDocParams.this
					.fireWizardListeners(WizardPageDocParams.CHANGE_PARAMS);
		}

		@Override
		public void change(JExpChangeElementEvent event) {
			WizardPageDocParams.this
					.fireWizardListeners(WizardPageDocParams.CHANGE_PARAMS);
		}
	}
}
